package com.beta.springmvc.controller;

import com.beta.springmvc.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.support.RequestContext;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * Created by yaoyt on 16/12/1.
 *
 * @author yaoyt
 */
@Controller
@RequestMapping("/form")
public class FormController {
    @RequestMapping("/show")
    public String show(){
        return "form/form";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(@Valid User u, BindingResult br,HttpServletRequest request) {
        RequestContext requestContext = new RequestContext(request);
        System.out.println(requestContext.getMessage("buttonName1"));
        if (br.getErrorCount() > 0) {
            return "form/form";
        }
        return "form/form";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(Map<String, Object> map) {
        User u = new User();
        u.setName("adfasdfas");
        map.put("user", u);
        return "form/form";
    }


}
